package lee.vioson.ebook.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import lee.vioson.ebook.R
import lee.vioson.ebook.data.AppContainer
import lee.vioson.ebook.ui.AppTheme
import lee.vioson.ebook.viewmodels.RecentlyViewModel

/**
 *Author:viosonlee
 *Date:2023/6/3
 *DESCRIPTION:
 */
@Composable
fun RecentlyScreen(
    navHostController: NavHostController,
    appContainer: AppContainer,
    viewModel: RecentlyViewModel = viewModel(factory = viewModelFactory {
        initializer {
            RecentlyViewModel(
                appContainer
            )
        }
    })
) {
    val recommendList = viewModel.recommendPageData.collectAsLazyPagingItems()
    val state by viewModel.state.collectAsState()
    LaunchedEffect(key1 = Unit, block = {
        viewModel.fetchRecentlyList()
    })

    MviScreen(viewModel = viewModel) {
        Column(
            Modifier
                .fillMaxSize()
                .statusBarsPadding()
        ) {
            HomeBookList(
                modifier = Modifier.padding(bottom = 16.dp),
                title = stringResource(id = R.string.Recently),
                list = state.recentlyList,
                navHostController = navHostController,
                editable = true
            )
            if (!state.isVip) {
                VipGuide(
                    modifier = Modifier
                        .padding(vertical = 16.dp, horizontal = 8.dp)
                        .fillMaxWidth()
                        .height(70.dp),
                    navHostController
                )
            }
            Row(
                Modifier.fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    modifier = Modifier.padding(8.dp),
                    text = stringResource(id = R.string.Recommended),
                    fontSize = 18.sp,
                    color = AppTheme.colors.onBackground
                )
            }
            LazyVerticalGrid(
                columns = GridCells.Fixed(3),
                modifier = Modifier.padding(horizontal = 13.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp),
                horizontalArrangement = Arrangement.spacedBy(10.dp),
                content = {
                    items(recommendList.itemSnapshotList.items) { book ->
                        BookCard(book = book, navHostController = navHostController)
                    }
                })
        }
    }
}